<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="" prop="account">
      <el-input v-model="dataForm.account" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="password">
      <el-input v-model="dataForm.password" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="role">
      <el-input v-model="dataForm.role" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="name">
      <el-input v-model="dataForm.name" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="phone">
      <el-input v-model="dataForm.phone" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="性别, 0：男, 1：女" prop="sex">
      <el-input v-model="dataForm.sex" placeholder="性别, 0：男, 1：女"></el-input>
    </el-form-item>
    <el-form-item label="入住宿舍时间" prop="checktime">
      <el-input v-model="dataForm.checktime" placeholder="入住宿舍时间"></el-input>
    </el-form-item>
    <el-form-item label="" prop="createdat">
      <el-input v-model="dataForm.createdat" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="updatedat">
      <el-input v-model="dataForm.updatedat" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="deletedat">
      <el-input v-model="dataForm.deletedat" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="roomid">
      <el-input v-model="dataForm.roomid" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="facultyid">
      <el-input v-model="dataForm.facultyid" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="majorid">
      <el-input v-model="dataForm.majorid" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          account: '',
          password: '',
          role: '',
          name: '',
          phone: '',
          sex: '',
          checktime: '',
          createdat: '',
          updatedat: '',
          deletedat: '',
          roomid: '',
          facultyid: '',
          majorid: ''
        },
        dataRule: {
          account: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          role: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          sex: [
            { required: true, message: '性别, 0：男, 1：女不能为空', trigger: 'blur' }
          ],
          checktime: [
            { required: true, message: '入住宿舍时间不能为空', trigger: 'blur' }
          ],
          createdat: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          updatedat: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          deletedat: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          roomid: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          facultyid: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          majorid: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/roomManager/users/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.account = data.users.account
                this.dataForm.password = data.users.password
                this.dataForm.role = data.users.role
                this.dataForm.name = data.users.name
                this.dataForm.phone = data.users.phone
                this.dataForm.sex = data.users.sex
                this.dataForm.checktime = data.users.checktime
                this.dataForm.createdat = data.users.createdat
                this.dataForm.updatedat = data.users.updatedat
                this.dataForm.deletedat = data.users.deletedat
                this.dataForm.roomid = data.users.roomid
                this.dataForm.facultyid = data.users.facultyid
                this.dataForm.majorid = data.users.majorid
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/roomManager/users/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'account': this.dataForm.account,
                'password': this.dataForm.password,
                'role': this.dataForm.role,
                'name': this.dataForm.name,
                'phone': this.dataForm.phone,
                'sex': this.dataForm.sex,
                'checktime': this.dataForm.checktime,
                'createdat': this.dataForm.createdat,
                'updatedat': this.dataForm.updatedat,
                'deletedat': this.dataForm.deletedat,
                'roomid': this.dataForm.roomid,
                'facultyid': this.dataForm.facultyid,
                'majorid': this.dataForm.majorid
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
